Make a couple minor changes to utils needed to migrate Google GenAi instrumentation to utils library#17
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors the GenAI utility's invocation lifecycle so GenAIInvocation (and its subclasses) implement the context manager protocol directly via __enter__/__exit__, allowing the existing handler.inference(), handler.embedding(), handler.tool(), handler.workflow(), handler.invoke_local_agent(), and handler.invoke_remote_agent() factory methods to return the invocation object itself — usable either manually or via with. The older handler.start_* factories are kept and marked deprecated to preserve back-compat. Also threads a tool_result constructor parameter and records gen_ai.tool.call.arguments / gen_ai.tool.call.result on tool spans, and migrates all tests to the new factory API.
Changes:
- Replace
_managed()contextmanager helper with__enter__/__exit__onGenAIInvocation; reworkhandler.*factories to return invocations directly (instead ofAbstractContextManager); deprecatestart_*factories via docstrings. - Add
tool_resultconstructor parameter toToolInvocationand emitGEN_AI_TOOL_CALL_ARGUMENTS/GEN_AI_TOOL_CALL_RESULTattributes at both start and finish. - Migrate test suite from
handler.start_*to the newhandler.*factory methods.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_invocation.py | Replace _managed contextmanager with __enter__/__exit__; clean up unused imports |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py | Make inference/embedding/tool/workflow/invoke_*_agent return invocations directly; deprecate start_* factories |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py | Update docstrings to point at new factory |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_embedding_invocation.py | Update docstrings to point at new factory |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_workflow_invocation.py | Update docstrings to point at new factory |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_agent_invocation.py | Update docstrings to point at new factories |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_tool_invocation.py | Add tool_result ctor param; emit tool args/result attributes |
| util/opentelemetry-util-genai/tests/test_utils.py | Switch tests from start_inference/start_embedding to new factories |
| util/opentelemetry-util-genai/tests/test_utils_events.py | Switch tests to handler.inference |
| util/opentelemetry-util-genai/tests/test_toolcall.py | Switch tests to handler.tool |
| util/opentelemetry-util-genai/tests/test_workflow_invocation.py | Switch tests to handler.workflow |
| util/opentelemetry-util-genai/tests/test_handler_workflow.py | Switch tests to handler.workflow |
| util/opentelemetry-util-genai/tests/test_handler_metrics.py | Switch tests to new inference/embedding/tool factories |
| util/opentelemetry-util-genai/tests/test_handler_completion_hook.py | Switch tests to new inference/workflow/invoke_*_agent factories |
| util/opentelemetry-util-genai/tests/test_handler_agent.py | Switch tests to handler.invoke_local_agent/invoke_remote_agent |
A couple minor changes needed to migrate Google GenAi over to the utils library..
Make
GenAiInvocationclass (and parent classes) a context manager by defining enter/exit methods.. This way it can be optionally used as a ContextManager like python'sfileobject, and we just need 1 method (invocation) instead of 2 (start_invocaton/invocation)Open questions:
What's the best way to deprecate these methods ? We could do a release of GenAi utils, then I could update all instrumentations to stop using these methods, then release those instrumentations, and then finally remove these methods. Or we could have a long deprecation period ? But that feels unnecessary..
Should we stop using
typing.Anytypes onToolInvocationand all these other classes ? IMO we should force instrumentation to serialize these to things that can go into the span/metric/log (like the OTEL Attributes type),Anyis too generic to serialize.. Also IMO the sem convs themselves should be more opinionated about how to serialize things..